package snake.controll;

import boogie.game.Game;
import boogie.game.sound.SoundManager;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import snake.PausedState;
import snake.PlayState;
import snake.snakeSprites.Direction;
import snake.snakeSprites.Snake;

/**
 *
 * @author Boogie
 */
public class SnakeController extends KeyAdapter {

    private final Snake snake;

    public SnakeController(Snake s) {
        this.snake = s;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        Direction d = null;
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                if (snake.getDirection() != Direction.RIGHT) {
                    d = Direction.LEFT;
                }
                break;
            case KeyEvent.VK_RIGHT:
                if (snake.getDirection() != Direction.LEFT) {
                    d = Direction.RIGHT;
                }
                break;
            case KeyEvent.VK_UP:
                if (snake.getDirection() != Direction.DOWN) {
                    d = Direction.UP;
                }
                break;
            case KeyEvent.VK_DOWN:
                if (snake.getDirection() != Direction.UP) {
                    d = Direction.DOWN;
                }
                break;
            case KeyEvent.VK_SPACE:
                Game g = snake.gameState.getGame();
                if(g.getCurrentState().equals(g.getState(PausedState.NAME))){
                    g.setCurrentState(g.getState(PlayState.NAME));
                }else{
                    g.setCurrentState(g.getState(PausedState.NAME));
                }
                break;
            case KeyEvent.VK_M:
                SoundManager m = SoundManager.getInstance();
                if(m.isMute()){
                    m.setMute(false);
                }else{
                    m.setMute(true);
                }
                break;
        }
        if (d != null && snake.getDirection()!=d) {
            snake.commands.add(new SnakeMoveCommand(snake, d));
            snake.setDirection(d);
        }

    }

}
